package Action.user;

import Bean.BookCommentEntity;
import Bean.BookInfoEntity;
import Bean.LoanBookLogRecord;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.hibernate.Session;
import tools.HibernateTools;

import javax.sound.midi.Track;
import java.util.LinkedList;
import java.util.List;

public class ManageComment extends ActionSupport {
    List<BookCommentEntity> bookCommentEntities;
    LinkedList<BookCommentEntity> data;
    int useid;
    String key;
    int modify_id;
    Session session;


    public String find(){
        try {
            useid = (int) ActionContext.getContext().getSession().get("userid");
        } catch (Exception e) {
           return LOGIN;
        }
        ;
        session = HibernateTools.getSession();
        //数据获取
        bookCommentEntities=session.createQuery("from BookCommentEntity where userId=?").setParameter(0,useid).getResultList();
        List<BookInfoEntity> books = session.createQuery("from BookInfoEntity where book_name like ?").setParameter(0, "%" + key + "%").getResultList();
        StringBuilder stringBuilder=new StringBuilder();
        for (BookInfoEntity book : books) {
            stringBuilder.append(book.getBook_name());
        }
        String book_string=stringBuilder.toString();
        data=new LinkedList<>();
        if (key!=null&&!key.equals("")){
            //评论筛选流程
            for (BookCommentEntity bookCommentEntity : bookCommentEntities) {
                //评论内容筛选
                if (bookCommentEntity.getComment()!=null&&bookCommentEntity.getComment().contains(key)){
                    data.add(bookCommentEntity);
                    continue;
                }
                if ((""+bookCommentEntity.getBookId()).contains(key)){
                    data.add(bookCommentEntity);
                    continue;
                }
                //书名模糊搜索
                if ((book_string.contains(key)&&book_string.contains(bookCommentEntity.bookName()))){
                    data.add(bookCommentEntity);
                }
            }
        }else {
            data.addAll(bookCommentEntities);
        }

        return SUCCESS;
    }
    public String del(){
        clearErrorsAndMessages();
        session = HibernateTools.getSession();
        BookCommentEntity bookCommentEntity=session.get(BookCommentEntity.class, modify_id);
        if (bookCommentEntity==null){
            addActionError("删除失败评论不存在");
            return find();
        }
        session.delete(bookCommentEntity);
        addActionMessage("删除成功");
        return find();
    }


    public LinkedList<BookCommentEntity> getData() {
        return data;
    }

    public void setData(LinkedList<BookCommentEntity> data) {
        this.data = data;
    }

    public int getUseid() {
        return useid;
    }

    public void setUseid(int useid) {
        this.useid = useid;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public int getModify_id() {
        return modify_id;
    }

    public void setModify_id(int modify_id) {
        this.modify_id = modify_id;
    }
}
